// miniprogram/pages/message/fileList/fileList.js
import store from '../../../store/index'
import create from '../../../utils/create'
import api from '../../../api/index'
create(store,{

  /**
   * 页面的初始数据
   */
  use:['user'],
  data: {
    files:[],
    friendID :'', //正在聊天人的id
    friendName: '陈大杰', // 正在和谁聊天
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   console.log(JSON.parse(options.files))
    this.setData({
      files:JSON.parse(options.files),
      friendName:options.name
    },()=>{
        console.log(this.data.friendName)
    })  

    this.loadFile();
  },
  loadFile:async function(e){
    const that = this
    return new Promise((reslove,reject)=>{
      var file = []
      var friList = []
      var friendID = this.data.friendID
      console.log(friendID);
      store.data.user.yourWorks.forEach(item => {
				if(item.file) {
					friList.push(item)
				}
			})
			store.data.user.yourResume.forEach(item => {
				if(item.file) {
					friList.push(item)
				}
			})
      // friList.push(...store.data.user.yourWorks)
      // friList.push(...store.data.user.yourResume)
      
      console.log(friList)
      for (let i = 0;i<friList.length;i++){
        file.push(new Promise((reslove,reject)=>{
          console.log("111")
          let op = friList[i].Myopenid
          if(op==friendID){
            console.log("222")
           console.log(friList[i].file)
            that.data.files.push(friList[i].file)
            that.setData({
              files:that.data.files
            },()=>{
              console.log(that.data.files)
            })
            reslove(friList[i].file)
          }
        }))
      }
      Promise.all(file).then(res=>{
        reslove(res)
      })
    })
  },
  open: async function(e){
    var index = e.currentTarget.dataset.index
    var file = this.data.files[index].file
    console.log(file);
    var fileList = []
    fileList.push(file)
    const params = {fileList}
    wx.showLoading({title: '打开中'})
    api.file.downloadResume(params).then(res=>{
      console.log(res.massage)
      wx.hideLoading()
    })
  },


})